<?php

class pm extends Model
{

	function countUnread( $stud_no ) {
		$count = $this->db->getAll( "SELECT COUNT(*) count 
				FROM pm 
				WHERE receiver_id = ?
				AND `read` = 0",
			array( $stud_no )
		);
		if( count($count) > 0 ) return $count[0]['count'];
	}

	function countInbox ( $stud_no)
	{
		$cinbox = $this->db->getAll("SELECT COUNT(*) count 
				FROM pm 
				WHERE receiver_id = ?",
			array( $stud_no)
		);
		
		return $cinbox[0]['count'];
	}
	
	function getInbox ( $stud_no )
	{
		$inbox = $this->db->getAll("SELECT pm.sender_id, 
				Sender.lastname,
				Sender.firstname,
				Sender.middlename,
				pm.message_id,
				pm.date,
				pm.subject,
				pm.read
			FROM pm, user Sender, user Receiver
			WHERE Sender.stud_no = pm.sender_id
			AND Receiver.stud_no = pm.receiver_id
			AND Receiver.stud_no = ? 
			ORDER BY pm.date DESC
			",
		array( $stud_no)
		);
		return $inbox;
	}

	function getMessagebyID ( $stud_no, $message_id )
	{
		$inbox = $this->db->getAll("SELECT pm.sender_id, 
				Sender.lastname,
				Sender.firstname,
				Sender.middlename,
				pm.date,
				pm.subject,
				pm.message_id,
				pm.message
			FROM pm, user Sender, user Receiver
			WHERE Sender.stud_no = pm.sender_id
			AND Receiver.stud_no = pm.receiver_id
			AND Receiver.stud_no = ? 
			AND pm.message_id = ? ",
		array( $stud_no, $message_id)
		);
		return $inbox;
	}
	
	
	function getsent ( $stud_no )
	{
		$sent = $this->db->getAll("SELECT pm.sender_id,		pm.receiver_id,
				Receiver.lastname,
				Receiver.firstname,
				Receiver.middlename,
				pm.date,
				pm.message_id,
				pm.subject,
				pm.message,
				pm.read
			FROM pm, user Sender, user Receiver
			WHERE Sender.stud_no = pm.sender_id
			AND Receiver.stud_no = pm.receiver_id
			AND Sender.stud_no = ? 
			ORDER BY pm.date DESC
			",
		array( $stud_no)
		);
		return $sent;
	}

	function getsentById ( $stud_no, $message_id )
	{
		$sent = $this->db->getAll("SELECT pm.sender_id,		pm.receiver_id,
				Receiver.lastname,
				Receiver.firstname,
				Receiver.middlename,
				pm.date,
				pm.message_id,
				pm.subject,
				pm.message
			FROM pm, user Sender, user Receiver
			WHERE Sender.stud_no = pm.sender_id
			AND Receiver.stud_no = pm.receiver_id
			AND Sender.stud_no = ? 
			AND pm.message_id = ? ",
		array( $stud_no, $message_id)
		);
		return $sent;
	}
	
	function insertPm( $sender_id, $receiver_id, $date, $subject, $message )
	{
		$ipm = $this->db->execute("INSERT INTO pm
			( sender_id,
			receiver_id, 
			date,
			subject,
			message)
			VALUES ( ?, ?, ?, ?, ? )", 
			array( $sender_id, $receiver_id, $date, $subject, $message )
		);
		return $ipm;
	}

	function updatePm( $sender_id, $receiver_id, $date, $subject, $message, $message_id ) 
	{
		$upm = $this->db->execute("UPDATE pm
			SET sender_id = ?,
				receiver_id = ?,
				date = ?,
				subject = ?,
				message = ?
			WHERE message_id = ?",
			array( $sender_id, $receiver_id, $date, $subject, $message, $message_id )
		);
		return $upm;
	}

	function deletePm ( $message_id )
	{
		$dpm = $this->db->execute("DELETE FROM pm
			WHERE message_id = ? ",
			array( $message_id ));
	}
	
	function deletePMs ( array $messageIds ) {
		$numMessages = count( $messageIds );
		for( $i=0; $i<$numMessages; $i++ ) {
			$messageIds[$i] = array( $messageIds[$i] );
		}
		$dpm = $this->db->executeMultiple(
			"DELETE FROM pm
			WHERE message_id = ?",
			$messageIds);
	}
	
	function read( $messageId ) {
		$this->db->execute("
				UPDATE pm
				SET `read` = 1
				WHERE message_id = ?",
			array( $messageId )
		);
	}
}
